package cc.mycraft.pvp;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;


public class RankingList {
	private static File RankFile;
	protected static FileConfiguration Rank;
    private Thread T;
    
    
    public static void run() {
    	RankFile = new File(Main.getPlugin().getDataFolder()+"/Rank.yml");
    	Rank = YamlConfiguration.loadConfiguration(RankFile);
    }
    public static void clearRanking() {
    	Rank.set("Rank", null);
    	Rank.set("LastUpdate", 0L);
		try {
			Rank.save(RankFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	RankFile = new File(Main.getPlugin().getDataFolder()+"/Rank.yml");
    	Rank = YamlConfiguration.loadConfiguration(RankFile);
    }
	public static void viewTop(Player p,String arg) {
		int num=Integer.parseInt(arg);
		/*
		try {
			num = Integer.parseInt(arg);
		} catch (Exception e) {
			p.sendMessage("请输入数字。");
			return;
		}
		*/
		int page = 10*num;
		int frontpage = page - 10;
		
		List<String> L = Rank.getStringList("Rank");
		
		if(page>L.size()) {
			page=L.size();
		}
		
		p.sendMessage("§e----------------------§6竞技勇者 "+(frontpage+1)+"-"+page+"/"+L.size()+"§e----------------------");
		for(int j=frontpage;j<page;j++) {
			p.sendMessage("§7"+L.get(j));
		}		
	}
    
	protected void setTopList(Player p) {
		long now = new Date().getTime();
		
		if(now - Rank.getLong("LastUpdate")<7200000&&!p.hasPermission("mycraft.pvp.admin")) {
			long need =7200L - ((now-Rank.getLong("LastUpdate"))/1000L);
			p.sendMessage("§e排行榜还需要"+need+"秒才能更新。");
			return;
		}
		
		//Set<String> Section = PlayerManager.PPoint.getConfigurationSection("Points").getKeys(false);
		Set<String> Section = PlayerManager.PPoint.getConfigurationSection("Points").getKeys(false);
		T = new Thread(new Runnable() {
			@Override
			public void run() {
				List<String> ResultList = new ArrayList<>();
				HashMap<String,Integer> hm = new HashMap<String,Integer>();
				//载入数据
				for(String playerSection:Section) {
					int Point = PlayerManager.PPoint.getInt("Points."+playerSection);
					hm.put(playerSection.substring(0, playerSection.length()-38), Point);
				}
				ByValueComparator bvc  =new ByValueComparator(hm);
				List<String> newList=new ArrayList<String>(hm.keySet());
				Collections.sort(newList, bvc);
				//制成好看的格式
				for(String str:newList) {
					str = (1+newList.indexOf(str))+". §3§l"+str+"    §e积分: §6§l"+hm.get(str)+" \n";
					ResultList.add(str);
				}
				Rank.set("LastUpdate", now);
				Rank.set("Rank", ResultList);
				try {
					Rank.save(RankFile);
				} catch (Exception e) {
					e.printStackTrace();
				}
				Bukkit.getLogger().info("竞技场排名已排序完成！");
				p.sendMessage("§e竞技场排名已排序完成！");
				return;
			}
		});
		T.start();
	}
	static class ByValueComparator implements Comparator<String> {

		HashMap<String, Integer> base_map;

		public ByValueComparator(HashMap<String, Integer> base_map) {
			this.base_map = base_map;
		}

		public int compare(String arg0, String arg1) {
			if (!base_map.containsKey(arg0) || !base_map.containsKey(arg1)) {
				return 0;
			}

			if (base_map.get(arg0) < base_map.get(arg1)) {
				return 1;
			}else if (base_map.get(arg0) == base_map.get(arg1)) {
				return 0;
			}else {
				return -1;
			}
		}
	}
}
